#include <mysql.h>
#include <string.h>
#include <stdio.h>

#define DB_HOST "115.145.171.44"
#define DB_USER "cost"
#define DB_PASS "simul722"
#define DB_NAME "Complier"
#define CHOP(x) x[strlen(x) - 1] = ' '

int main(void)
{
    MYSQL       *connection=NULL, conn;
    MYSQL_RES   *sql_result;
    MYSQL_ROW   sql_row;
    int       query_stat;

    char id[20];
    char query[1024];
    char username[20];
    char password[20];

    mysql_init(&conn);

    connection = mysql_real_connect(&conn, DB_HOST,
                                    DB_USER, DB_PASS,
                                    DB_NAME, 3306,
                                    (char *)NULL, 0);

    if (connection == NULL)
    {
        fprintf(stderr, "Mysql connection error : %s\n", mysql_error(&conn));
        return 1;
    }

    // Check Input Patterns	
    strcpy(username, "skku1");
    strcpy(password, "pass1");

    // Check Query Patterns
    sprintf(query, "select * from member where username='%s' and password='%s'", username, password);

    query_stat = mysql_query(connection, query);
    //query_stat = mysql_query(connection, "select * from Member where username='skku1' and password = 'pass1' OR 'x'='x'");
    if (query_stat != 0)
    {
        fprintf(stderr, "Mysql query error : %s\n", mysql_error(&conn));
        return 1;
    }

    sql_result = mysql_store_result(connection);

    printf("%+11s %-30s", "Username", "Password");
    printf("\n");
    while ( (sql_row = mysql_fetch_row(sql_result)) != NULL )
    {
        printf("%+11s %-30s", sql_row[0], sql_row[1]);
        printf("\n");
    }

    mysql_free_result(sql_result);


    mysql_close(connection);
}
